package com.newsproject.controller;


import com.newsproject.pojo.DTO.UpdatePost;
import com.newsproject.pojo.Result;
import com.newsproject.pojo.vo.PublishPost;
import com.newsproject.service.PostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
@Api(tags = "创作者中心")
public class ManageController {

    @Autowired
    private PostService postService;

    @GetMapping("creator")
    @ApiOperation("创作者中心")
    @ApiResponses(value = {
            @ApiResponse(code = 200,message="posts,全部新闻，user ，用户信息 ，count ,数量")
    })
    public ModelAndView manage(HttpSession session) {
       return  postService.redirectToCreator(session);
    }


    @PostMapping("/updatePost/{postId}")
    @ApiOperation("修改文章")
    @ResponseBody
    public Result<Object> updatePost(@ModelAttribute PublishPost post,
                                                 @RequestParam(value = "coverImage",required = false) MultipartFile coverImage,
                                                 @RequestParam(value = "picture",required = false) MultipartFile picture,
                                                 @PathVariable("postId")Integer postId) throws Exception {
        return postService.updatePost(post,coverImage,picture,postId);
    }

    @GetMapping("/getPostById/{postId}")
    @ApiOperation("根据文章id获取文章")
    @ResponseBody
    public Result<UpdatePost> getPostById(@PathVariable("postId") Integer postId) {
        return postService.getPostById(postId);
    }




}
